<!--
 * @Author: 袁三金
 * @Date: 2024-03-18 21:50:34
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-03-20 22:30:10
 * @Description: 全部照片页面
-->
<template>
  <div class="all">
    <header class="header">
      <div class="header_left">全部照片</div>
    </header>
    <div class="image_main">
      <div
        class="demo-image__preview"
        v-for="(item, index) in imageList"
        :key="index"
        :class="{ active: activeIndex === index }"
        @contextmenu="showContextMenu(index, $event)"
        @click="hideContextMenu"
      >
        <div
          class="context_menu"
          v-if="activeIndex === index"
          :style="{ top: contextMenuTop + 'px', left: contextMenuLeft + 'px' }"
        >
          <ul class="context_menu_items">
            <li class="context_menu_item" @click.stop="downloadopen(item)">
              <div class="context_menu_item_icon">
                <i class="el-icon-folder-add"></i>
              </div>
              <div class="context_menu_item_text">下载</div>
            </li>
            <li class="context_menu_item" @click.stop="removeflagopen(item)">
              <div class="context_menu_item_icon">
                <i class="el-icon-upload2"></i>
              </div>
              <div class="context_menu_item_text" >
                添加到相册
              </div>
            </li>
            <li class="context_menu_item waring" @click.stop="deleteByid(item)">
              <div class="context_menu_item_icon">
                <i class="el-icon-refresh"></i>
              </div>
              <div class="context_menu_item_text" >
                删除
              </div>
            </li>
          </ul>
        </div>
        <el-image style="width: 170px; height: 170px" :src="item.imgUrl">
        </el-image>
      </div>
    </div>
    <div class="upload" @click="uploadDialog = true">+</div>
    <el-dialog
      title="图片上传"
      :visible.sync="uploadDialog"
      width="408px"
    >
      <el-upload
          id="upload"
          ref="upload"
          :auto-upload="false"
          :http-request="handleFileUpload"
        class="upload-demo"
        drag
        action="#"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
        </div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="uploadDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirm()"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!--    下载-->
    <el-dialog
        title="下载"
        :visible.sync="downflog"
        width="30%">
      <span style="color: red">必须是盘符中存在的路径</span>
      <span></span>
      <el-input style="margin-top: 10px" v-model="downloadp" placeholder="请输入内容"></el-input>
      <span slot="footer" class="dialog-footer">
    <el-button @click="downflog = false">取 消</el-button>
    <el-button type="primary" @click="doenloadOne()">确 定</el-button>
  </span>
    </el-dialog>
<!--一移动到相册-->
    <el-dialog
        title="移动到相册"
        :visible.sync="removeflag"
        width="30%">
      <span>请选择需要移动到的相册： </span>
      <el-select v-model="value" placeholder="请选择">
        <el-option
            v-for="item in xc"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
    <el-button @click="removeflag = false">取 消</el-button>
    <el-button type="primary" @click="remove()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import {getImage, downloadImage, deleteById, getxc, remove} from "@/api/image";
import {downloadOne} from "@/api/resource";

export default {
  data() {
    return {
      uploadDialog: false,
      imageList: [

      ],
      activeIndex: -1,
      contextMenuLeft: 0, // 右键菜单的左边距
      contextMenuTop: 0, // 右键菜单的上边距
      downflog: false,
      downloadp: "D:/doc/clouddisk",
      downloadFileUrl:"",
      removeflag: false,
      xc: [],
      value: "",
      id:"",

    };
  },
  computed: {
    isContextMenuVisible() {
      return this.activeIndex !== -1; // 判断右键菜单是否可见
    },
  },
  mounted() {
    document.addEventListener("click", this.hideContextMenu); // 监听点击事件，用于隐藏右键菜单
  },
  created() {
    this.getList()
  },
  methods: {
    removeflagopen(item){
      this.removeflag = true
      this.id = item.id
      this.getxc()
    },
    remove(){
      remove(this.id,this.value).then(res => {
        if (res.code === 200){
          this.$message.success("移动成功")
          this.removeflag = false
        }
      })
    },
    // 获取相册
    getxc(){
      getxc().then(res => {
        this.xc = res.data
      })
    },
    getList(){
      getImage().then(res => {
        this.imageList = res.data
      })
    },
    async handleFileUpload(e) {
      try {
        let formData = new FormData()
        formData.append('file', e.file)
        let res
          res = await request({
            url: 'http://localhost:8080/image/upload',
            method: "post",
            data: formData,
            params: {
              pId: this.id
            }
          })

        if (res.code === 200) {
          console.log('上传成功', res)
          this.getList()
          this.$message.success(res.msg)

          this.uploadDialog = false
        }
      } catch (e) {

      }
    },
    confirm() {
      this.$emit('confirm')
      this.$refs.upload.submit()

    },
    // 下载
    downloadopen(item){
      this.downflog = true
      this.downloadFileUrl = item.imgUrl
    },
    doenloadOne(){
      downloadImage(this.downloadp,this.downloadFileUrl).then(res => {
        if (res.code === 200){
          this.$message.success("下载成功")
          this.downflog = false
        }
      })
    },
    // 删除
    deleteByid(item){
      deleteById(item.id).then(res => {
        if (res.code === 200){
          this.$message.success("删除成功")
          this.getList()
        }
      })
    },    showContextMenu(index, event) {
      event.preventDefault(); // 阻止默认的右键菜单弹出
      this.activeIndex = index; // 设置当前激活的索引为点击的索引
      this.contextMenuLeft = event.clientX; // 获取鼠标点击的水平位置
      this.contextMenuTop = event.clientY; // 获取鼠标点击的垂直位置
    },
    hideContextMenu() {
      this.activeIndex = -1; // 重置当前激活的索引
    },
  },
};
</script>

<style lang="scss" scoped>
.all {
  width: 100%;
  height: 100%;
  display: flex;
  padding: 0 40px;
  padding-top: 24px;
  flex-direction: column;
  position: relative;
  .header {
    display: flex;
    margin-bottom: 20px;
    align-items: center;
  }
  .header_left {
    font-size: 18px;
    font-weight: 600;
    color: rgb(37, 38, 43);
  }
  .upload {
    border-radius: 50%;
    width: 60px;
    height: 60px;
    background-color: rgb(99, 125, 255);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 40px;
    font-weight: 100;
    cursor: pointer;
    position: absolute;
    bottom: 50px;
    right: 50px;
  }
  .upload:hover {
    opacity: 0.9;
  }
  .upload-demo{
    margin: 0 auto;
  }
  .image_main {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;

    .demo-image__preview {
      height: 170px;
      width: 170px;
      margin-right: 10px;
      position: relative;
      .context_menu {
        position: fixed;
        z-index: 999;
        width: 150px;
        background-color: #fff;
        box-shadow: 0 2px 4px rgba(0, 13, 0, 0.5);
        border-radius: 5px;
        padding: 4px;
        .context_menu_items {
          list-style: none;
          padding: 0;
          margin: 0;

          .context_menu_item {
            height: 36px;
            border-radius: 5px;
            padding: 4px 4px;
            display: flex;
            align-items: center;

            .context_menu_item_icon {
              font-size: 16px;
              margin-right: 5px;
            }

            .context_menu_item_text {
              font-size: 15px;
              color: #504e4e;
            }
          }
          .context_menu_item.waring {
            color: rgb(255, 111, 102);
          }
          .context_menu_item.waring:hover {
            background-color: rgb(243, 91, 81, 0.12);
          }
          .context_menu_item:hover {
            background-color: rgba(132, 133, 141, 0.08);
          }
        }
      }
    }
  }
}
</style>
